import Vue from "vue";
import Vuex from "vuex";
import {reqShopCart} from "@/api";

Vue.use(Vuex);

// 数据
let state = {
    shopCartList:[]//三级联动列表
};

// 写好逻辑后，提交给mutations处理修改数据
let actions = {
    async reqShopCart(context,params){
        let result = await reqShopCart();
        if ((result.data.code == 200)) {
            context.commit('REQSHOPCART',result.data.data);
        }else {
            console.error('请求购物车列表失败');
            return Promise.reject(new Error("faile"));
        }
    },

};

// 只有mutations能够修改数据
let mutations = {
    REQSHOPCART(state,value){
        state.shopCartList = value;
    },

};

// 优化数据结构，过滤数据
let getters = {
    shopCartList(state){
        return state.shopCartList[0] || {};
    },
    cartInfoList(state,getters){
        return getters.shopCartList.cartInfoList || [];
    }
};


export default {
    namespaced:true,
    state,
    mutations,
    actions,
    getters
}
